package com.ymatou.promotion.api.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.ChangeSellerCouponBean;
import com.ymatou.promotion.api.service.ChangeSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.ProductCallService;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponScenarioWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 编辑商家优惠券
 * 
 * 用户指定批次对应的商品
 *
 * @author chenjiazhu
 */
@TestSuite
public class Ts_ChangeSellerCoupon {
    private static ChangeSellerCouponBean changesellercouponBean;
    private static ChangeSellerCouponCall changesellercouponCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("编辑商家优惠券");
    }

    @Before
    public void caseUp() {
        changesellercouponBean = new ChangeSellerCouponBean();
        changesellercouponCall = new ChangeSellerCouponCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(changesellercouponCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_001() {
        Logger.start(true, "编辑商家优惠券对应单个商品");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon();

            setChangeSellerCouponBean(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_002() {
        Logger.start(true, "编辑商家优惠券对应多个商品");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon();

            setChangeSellerCouponBeanForMultipleProduct(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_003() {
        Logger.start(true, "编辑洋码头PC端商品优惠券对应单个商品");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("1");

            setChangeSellerCouponBean(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_004() {
        Logger.start(true, "编辑洋码头PC端商品优惠券对应多个商品");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("1");

            setChangeSellerCouponBeanForMultipleProduct(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_005() {
        Logger.start(true, "编辑洋码头WAP端商品优惠券对应单个商品");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("2");

            setChangeSellerCouponBean(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_006() {
        Logger.start(true, "编辑洋码头WAP端商品优惠券对应多个商品");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("2");

            setChangeSellerCouponBeanForMultipleProduct(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_007() {
        Logger.start(true, "编辑洋码头APP端商品优惠券对应单个商品");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("3");

            setChangeSellerCouponBean(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_008() {
        Logger.start(true, "编辑洋码头APP端商品优惠券对应多个商品");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("3");

            setChangeSellerCouponBeanForMultipleProduct(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_009() {
        Logger.start(true, "编辑神器APP端商品优惠券对应单个商品");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("4");

            setChangeSellerCouponBean(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_010() {
        Logger.start(true, "编辑神器APP端商品优惠券对应多个商品");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("4");

            setChangeSellerCouponBeanForMultipleProduct(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_011() {
        Logger.start(true, "编辑神器WAP端商品优惠券对应单个商品");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("5");

            setChangeSellerCouponBean(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_012() {
        Logger.start(true, "编辑神器WAP端商品优惠券对应多个商品");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("5");

            setChangeSellerCouponBeanForMultipleProduct(batchCode);

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            VerifyReturn(batchCode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ChangeSellerCoupon_015() {
        Logger.start(false, "验证BatchCode空时报错");
        try {

            Map productInfoMap = new ProductCallService().createProduct(
                    Integer.parseInt(EnvSetup.getData("SellerId")), 1);
            changesellercouponBean.setProductCodes(productInfoMap.get(
                    "productId").toString());

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            Logger.verifyEquals("201", changesellercouponCall.getCode(),
                    "验证返回code,BatchCode是必填项，需跟开发确认下");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_016() {
        Logger.start(false, "验证ProductCodes空时报错");
        try {
            Map item = new YmtCouponBatchWapper().getTop1CouponBatch().get(0);
            changesellercouponBean.setBatchCode(item.get("sBatchCode")
                    .toString());

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            Logger.verifyEquals("200", changesellercouponCall.getCode(),
                    "验证返回code，没有报错，有问题，需跟开发确认下");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ChangeSellerCoupon_017() {
        Logger.start(false, "验证BatchCode不存在时报错");
        try {

            changesellercouponBean.setBatchCode("1123");
            Map productInfoMap = new ProductCallService().createProduct(
                    Integer.parseInt(EnvSetup.getData("SellerId")), 1);
            changesellercouponBean.setProductCodes(productInfoMap.get(
                    "productId").toString());

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            Logger.verifyEquals("201", changesellercouponCall.getCode(),
                    "验证返回code,BatchCode是必填项，需跟开发确认下");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeSellerCoupon_018() {
        Logger.start(false, "验证ProductCodes不存在时报错");
        try {
            Map item = new YmtCouponBatchWapper().getTop1CouponBatch().get(0);
            changesellercouponBean.setBatchCode(item.get("sBatchCode")
                    .toString());

            changesellercouponBean.setProductCodes("2344");

            changesellercouponCall.setData(changesellercouponBean);
            changesellercouponCall.callService();

            Logger.verifyEquals("200", changesellercouponCall.getCode(),
                    "验证返回code，没报错，有问题，需跟开发确认下");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 创建单个商品，设置Bean参数
     * 
     * @param batchCode
     */
    private void setChangeSellerCouponBean(String batchCode) {
        Logger.comment("创建单个商品，根据batchCode以及商品的Id设置Bean参数");

        changesellercouponBean.setBatchCode(batchCode);
        Map productInfoMap = new ProductCallService().createProduct(
                Integer.parseInt(EnvSetup.getData("SellerId")), 1);
        changesellercouponBean.setProductCodes(productInfoMap.get("productId")
                .toString());
    }

    /**
     * 创建单个商品，设置Bean参数
     * 
     * @param batchCode
     */
    private void setChangeSellerCouponBeanForMultipleProduct(String batchCode) {
        Logger.comment("创建两个商品，根据batchCode以及商品的Id列表设置Bean参数");

        changesellercouponBean.setBatchCode(batchCode);

        Map productInfoMap1 = new ProductCallService().createProduct(
                Integer.parseInt(EnvSetup.getData("SellerId")), 1);
        Map productInfoMap2 = new ProductCallService().createProduct(
                Integer.parseInt(EnvSetup.getData("SellerId")), 1);
        String productCodes = productInfoMap1.get("productId").toString() + ","
                + productInfoMap2.get("productId").toString();

        changesellercouponBean.setProductCodes(productCodes);
    }

    /**
     * 创建单个商品，根据返回的id，并取Ymt_CouponBatch表中最新一条记录中的batchcode，作为参数设置请求Bean
     * 
     * @param batchCode
     */
    private void setChangeSellerCouponBean() {
        Logger.comment("创建单个商品，根据返回的id，并取Ymt_CouponBatch表中最新一条记录中的batchcode，作为参数设置请求Bean");
        Map item = new YmtCouponBatchWapper().getTop1CouponBatch().get(0);

        changesellercouponBean.setBatchCode(item.get("sBatchCode").toString());
        Map productInfoMap = new ProductCallService().createProduct(
                Integer.parseInt(EnvSetup.getData("SellerId")), 1);
        changesellercouponBean.setProductCodes(productInfoMap.get("productId")
                .toString());

    }

    private void VerifyReturn(String batchCode) {
        Logger.verifyEquals("200", changesellercouponCall.getCode(), "验证返回code");
        Logger.verifyEquals("0", changesellercouponCall.getBCode(), "验证返回Bcode");
        Logger.verifyEquals("", changesellercouponCall.getMsg(), "验证返回Msg");

        YmtCouponScenario ymtCouponScenario = new YmtCouponScenarioWapper()
                .selectBysBatchCode(batchCode).get(0);

        Logger.verifyEquals(changesellercouponBean.getProductCodes(),
                ymtCouponScenario.getSspecificproducts(),
                "验证数据库中表YmtCouponScenario中batchCode=" + batchCode
                        + "的记录的Sspecificproducts值");

    }
}
